﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace CL2036.Model
{
    /// <summary>
    /// 系统配置
    /// </summary>
    public class SystemConfig
    {
        private Dictionary<int, int> RouteChannelCol = new Dictionary<int, int>();

        private int m_MeterBwCount = 12;

        private int m_ChannelCount = 1;

        private int m_ThreadCount = 0;

        private int m_2036Channel = 32;

        private string m_2018IP = "193.168.18.1";

        private bool m_Is188H = false;

        private bool m_IsDx = false;

        private const string CONST_Config = @"Plugins\\DeviceConfig.xml";

        /// <summary>
        /// 表位数
        /// </summary>
        public int MeterBwCount
        {
            get { return m_MeterBwCount; }
            set { m_MeterBwCount = value; }
        }
        /// <summary>
        /// 485通道数
        /// </summary>
        public int ChannelCount
        {
            get { return m_ChannelCount; }
            set { m_ChannelCount = value; }
        }
        /// <summary>
        /// 开启线程数
        /// </summary>
        public int ThreadCount
        {
            get { return m_ThreadCount; }
            set { m_ThreadCount = value; }
        }
        /// <summary>
        /// 2036通道号
        /// </summary>
        public int CL2036Channel
        {
            get { return m_2036Channel; }
            set { m_2036Channel = value; }
        }
        /// <summary>
        /// 获取或设置2018-1IP地址
        /// </summary>
        public string CL20181IP
        {
            get { return m_2018IP; }
            set { m_2018IP = value; }
        }

        /// <summary>
        /// 是不是188H模式
        /// </summary>
        public bool Is188H
        {
            get { return m_Is188H; }
            set { m_Is188H = value; }
        }
        /// <summary>
        /// 是否是单相台
        /// </summary>
        public bool IsDx
        {
            get { return m_IsDx; }
            set { m_IsDx = value; }
        }

        /// <summary>
        /// 获取线路对应通道
        /// </summary>
        /// <param name="RouteNo"></param>
        /// <returns></returns>
        public int RouteChannel(int RouteNo)
        {
            if (RouteChannelCol.ContainsKey(RouteNo))
            {
                return RouteChannelCol[RouteNo];
            }
            else
            {
                return RouteNo;
            }
        }
        /// <summary>
        /// 设置线路对应通道
        /// </summary>
        /// <param name="RouteNo">线路号</param>
        /// <param name="ChannelNo">通道号</param>
        /// <returns></returns>
        public void SetRouteChannel(int RouteNo, int ChannelNo)
        {
            if (RouteChannelCol.ContainsKey(RouteNo))
            {
                RouteChannelCol.Remove(RouteNo);
            }

            RouteChannelCol.Add(RouteNo, ChannelNo);
        }
        /// <summary>
        /// 存储配置
        /// </summary>
        public void Save()
        {

            string path = System.Environment.CurrentDirectory + @"\" + CONST_Config;

            XDocument xDoc = new XDocument();

            xDoc.Add(new XElement("Device"));

            XElement xe = xDoc.Element("Device");

            xe.Add(new XElement("Item", new XAttribute("Name","BwCount"),new XAttribute("Value", m_MeterBwCount.ToString()))); //表位数
            xe.Add(new XElement("Item", new XAttribute("Name","ChannelCount"),new XAttribute("Value", m_ChannelCount.ToString())));      //通道数
            xe.Add(new XElement("Item", new XAttribute("Name","ThreadCount"),new XAttribute("Value", m_ThreadCount.ToString())));        //线程数
            xe.Add(new XElement("Item", new XAttribute("Name","Cl2036"),new XAttribute("Value", m_2036Channel.ToString())));             //2036通道号
            xe.Add(new XElement("Item", new XAttribute("Name","Cl2018"),new XAttribute("Value", m_2018IP)));             //2018-1ip
            xe.Add(new XElement("Item", new XAttribute("Name","Is188H"),new XAttribute("Value", m_Is188H ? "1" : "0")));                  //是不是188H
            xe.Add(new XElement("Item", new XAttribute("Name","IsDx"),new XAttribute("Value", m_IsDx ? "1" : "0")));                  //是不是单相台

            int[] TmpKey = new int[RouteChannelCol.Keys.Count];

            RouteChannelCol.Keys.CopyTo(TmpKey, 0);

            for (int i = 0; i < TmpKey.Length; i++)
            {
                if (TmpKey[i] > m_ChannelCount) RouteChannelCol.Remove(TmpKey[i]);
            }

            for (int i = 0; i < m_ChannelCount; i++)
            {
                if (!RouteChannelCol.ContainsKey(i + 1)) RouteChannelCol.Add(i + 1, i + 1);
            }

            foreach (int Key in RouteChannelCol.Keys)
            {
                xe.Add(new XElement("Item", new XAttribute("Name",string.Format("线路{0}", Key)),new XAttribute("Value", RouteChannelCol[Key].ToString())));             //485通道
            }

            xDoc.Save(path);

        }


        public void Load()
        {
            string path = System.Environment.CurrentDirectory + @"\" + CONST_Config;

            if (!System.IO.File.Exists(path)) return;

            XDocument xDoc;

            XElement xe;

            try
            {
                xDoc = XDocument.Load(path);

                xe = xDoc.Element("Device");
            }
            catch { return;}
            
            m_MeterBwCount = int.Parse(getConfigValue(xe,"BwCount", m_MeterBwCount.ToString()));           //表位数

            m_ChannelCount = int.Parse(getConfigValue(xe, "ChannelCount", m_ChannelCount.ToString()));      //通道数

            m_ThreadCount = int.Parse(getConfigValue(xe,"ThreadCount", m_ThreadCount.ToString()));        //线程数

            m_2036Channel = int.Parse(getConfigValue(xe,"Cl2036", m_2036Channel.ToString()));             //2036通道号

            m_2018IP = getConfigValue(xe,"CL2018", m_2018IP);           //2018-1通信IP

            m_Is188H = getConfigValue(xe,"Is188H", m_Is188H ? "1" : "0") == "1" ? true : false;                  //是不是188H

            m_IsDx = getConfigValue(xe,"IsDx", m_IsDx ? "1" : "0") == "1" ? true : false;                  //是不是单相台

            for (int i = 0; i < m_ChannelCount; i++)
            {
                string TmpValue = getConfigValue(xe,string.Format("线路{0}", i + 1), (i + 1).ToString());

                RouteChannelCol.Add(i + 1, int.Parse(TmpValue));
            }

        }

        private string getConfigValue(XElement xe, string key, string defaultValue)
        {
            try
            {
                return (from element in xe.Descendants() 
                            where element.Attribute("Name").Value == key 
                                select element)
                        .First().Attribute("Value").Value; 
            }
            catch { return defaultValue; }
        }

    }
}
